<?php
namespace Admin\Controller;
use Admin\Model\managerModel;
use Admin\Model\roleModel;
use Admin\Model\memberModel;

class ManagerController extends BaseController {
    
    public $managerModel;
    public $roleModel;
    
    public function __construct() {
        parent::__construct();
        $this->managerModel = new managerModel();
        $this->roleModel = new roleModel();
    }
    
    /*
     * 删除
     */
    public function del(){
        
        if($this->admininfo['is_super'] != 1){
//            $this->error("抱歉,仅超级管理员才可以编辑系统信息");
        }
    
        $id = get("id");
        $where = array(
            "adminuid" => $id
        );
        
        if(!$id || $id==1){
            return_json(1,"当前账号不可删除");
        }
        
        $res = $this->managerModel->where($where)->delete();
        if($res){
            return_json(0,"删除成功");
        }
        return_json(1,"删除失败");

    }
    
    /*
     * 管理员列表
     */
    public function index(){
        
        $where = array();
        $manager_list = $this->managerModel->getManagerList($where);
        $this->assign('datalist',$manager_list);

        $this->display();
    }
    
    /*
     * 添加用户
     */
    public function addmanager(){
        
        if($this->admininfo['is_super'] != 1){
//            $this->error("抱歉,仅超级管理员才可以编辑系统信息");
        }
        
        
        if(IS_AJAX){
            $data =  param_filter($_POST,array('password','username','uid','only_read'),array('username'),$filter=true,true);//过滤数据
            if(empty($data)){
                return_json(1,"请填写信息");
            }
            
            if($data['role_id'] == -1){
                return_json(1,"请选择角色");
            }
            
            //判断是否已经存在
            $where = array(
                "username" => $data['username']
            );
            $r = $this->managerModel->getManager($where);
            
            if(isset($data['uid']) && !empty($data['uid'])){
                //有填写uid
                $memberModel = new memberModel();
                $where_u = array(
                    'uid'=>$data['uid'],
                    "status" =>1
                );
                $u = $memberModel->getMember($where_u);
                if(empty($u)){
                    return_json(1,"商城ID填写错误或该ID用户已被禁用");
                }
                
                $data['nickname'] = $u['realname'] ? $u['realname'] : $u['nickname'];
                $data['headimgurl'] = $u['headimgurl'];
                
                
            }else{
                $data['uid'] = 0;
                $data['nickname'] = '';
                $data['headimgurl'] = '';
            }
            $data['menu'] = implode($_POST['menu'], ',');
            $data['sub_menu'] = implode($_POST['submenu'], ',');
            //更新账号
            if($r){
                //管理员账号已经存在,更新

                if(!empty($data['password'])){
                    checkPassword($data['password']);
                    $data['password'] = md5($data['password'].C("SALT"));
                }else{
                    unset($data['password']);
                }
                
                $res = $this->managerModel->editManager($where, $data);
                if($res !== false){
                    return_json(0,"后台账号编辑成功","","/admin/manager/index");
                }
                return_json(1,"后台账号编辑失败");
            }
            if(empty($data['password'])){
                return_json(1,"密码不能为空");
            }
            checkPassword($data['password']);
            $data['password'] = md5($data['password'].C("SALT"));
            
           
            
            $res = $this->managerModel->add($data);
            if($res){
                return_json(0,"后台账号添加成功","","/admin/manager/index");
            }

            return_json(1,"添加后台账号失败");
        }
        
        $id = get("id");
        if($id){
            $where = array(
                "adminuid" => $id
            );
            $minfo = $this->managerModel->getManager($where);
            $this->assign("minfo",$minfo);
        }
        
//        //获取角色列表
//        $where = array();
//        $role_list = $this->roleModel->getRoleList($where);
//        $this->assign('datalist',$role_list);
        $menuArr = explode(',', $minfo['menu']);
        $submenuArr = explode( ',',$minfo['sub_menu']);
        //$menu = $menuModel->where(array('level'=>1))->select();
        $submenu = $this->menuModel->getMenuList(array('level'=>array('neq',1)));
        $new = array();
        foreach ($submenu as $k=>$v) {
            $v['checked'] = 0;
            if(in_array($v['id'], $submenuArr)){
                $v['checked'] = 1;
            }
            $new[$v['pid']]['submenu'][] = $v;
        }
        foreach ($new as $k=>$v) {
            $m = $this->menuModel->findMenuById($v['submenu'][0]['pid']);
            $m['checked'] = 0;
            if(in_array($m['id'], $menuArr)){
                $m['checked'] = 1;
            }
            $m['all'] = 0;
            $new[$k]['menu'] = $m;
        }
        $this->assign('menus', json_encode($new));
        
        $this->display();
    }
    
    /*
     * 角色列表
     */
    public function rolelist(){
        $where = array();
        $role_list = $this->roleModel->getRoleList($where);
        $this->assign('datalist',$role_list);
        $this->display();
    }
    
    /*
     * 添加角色
     */
    public function addrole(){
        
        if($this->admininfo['is_super'] != 1){
            $this->error("抱歉,仅超级管理员才可以编辑系统信息");
        }
        
        $this->display();
    }
    
    /*
     * 更改密码
     */
    public function changepassword(){
        
        if(IS_AJAX){
            $password = post("password");
            $newpassword = post("newpassword");
            $newpassword2 = post("newpassword2");
            
            if($this->admininfo['password'] != md5($password.C('SALT'))){
                return_json(1,"旧密码输入错误");
            }
            
            if($newpassword != $newpassword2){
                return_json(1,"两次密码不一致");
            }
            
            if(empty($newpassword)){
                return_json(1,"密码不能为空");
            }
            
            $where = array(
                "adminuid" => $this->admininfo["adminuid"]
            );
            $data = array(
                "password" => md5($newpassword2.C('SALT')),
                'try_time' => 0
            );
            
            $res = $this->managerModel->editManager($where, $data);

            if($res !== false){
                return_json(0,"密码修改成功","","/admin/manager/index");
            }
            
            return_json(1,"修改失败");
            
        }
        
        $this->display();
    }
    
    
}